package com.example.friendmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class myhelper extends SQLiteOpenHelper {

    public static final String FRIEND_INFO="friend_info";
    public static final String ID="_id";
    public static final String FRIEND_NAME="friend_name";
    public static final String FRIEND_SEX="friend_sex";
    public static final String FRIEND_PHONE="friend_phone";
    public static final String FRIEND_AGE="friend_age";

    public myhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    //创建数据库，只会执行一次
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table friend_info(_id integer primary key autoincrement,"+
                "friend_name text not null,"+
                "friend_sex text not null,"+
                "friend_phone text not null,"+
                "friend_age text not null)");
    }

    //数据库升级后会执行此方法
    //SQLiteDatabase db  SQLite访问工具
    //int oldVersion旧版本号
    //int newVersion新版本号
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }

    //添加数据
    public boolean insert(ContentValues values){
        //insert()会返回插入成功后的数据字段id
        return getWritableDatabase().insert(FRIEND_INFO,null,values)!=-1;
    }


}
